from appium import webdriver     # 导入webdriver模块
import time,random

class yibi_test1():
    def test01(self):
        desired_caps={}  #定义webdriver的兼容性设置字典对象
        desired_caps['platformName']='Android' #指定测试平台为Android
        desired_caps['platformVersion']='4.4.2'  # 指定移动端的版本号
        desired_caps['deviceName']='Appium'  # 指定设备名称
        desired_caps['appPackage']='com.mobivans.onestrokecharge'  # 指定要启动的包
        desired_caps['appActivity']='com.stub.stub01.Stub01'  # 指定启动的主类程序
        desired_caps['udid']='127.0.0.1:62001'   # 指定模拟器设备编号(adb devices输出结果)
        # desired_caps['unicodeKeyboard'] = 'True'  # 指定可输入中文

        # 实例化webdriver，并指定appium服务器访问地址
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
        time.sleep(2)
        # 模拟手工任意一笔记账
        driver.find_element_by_xpath("//android.widget.LinearLayout[@resou"
                                     "rce-id='com.mobivans.onestrokecharge:id/main_write1']/and"
                                     "roid.widget.ImageView[1]").click()     #点击记一笔
        time.sleep(2)
        index = random.randint(1,27)
        driver.find_element_by_xpath("//android.support.v7.widget.RecyclerView[@resou"
                                     "rce-id='com.mobivans.onestrokecharge:id/add_rv_cateG"
                                     "rid']/android.widget.LinearLayout[%d]/a"
                                     "ndroid.widget.ImageView[1]"%index).click()    #点击任意类型
        time.sleep(2)
        driver.find_element_by_id("com.mobivans.onestrokecharge:id/keyb_btn_4").click()  # 点击4
        time.sleep(2)
        driver.find_element_by_id('com.mobivans.onestrokecharge:id/keyb_btn_9').click()  # 点击9
        time.sleep(2)
        driver.find_element_by_id("com.mobivans.onestrokecharge:id/keyb_btn_finish").click()   # 点击完成


if __name__ == '__main__':
    t=yibi_test1()
    t.test01()
